package com.example.yin.service.impl;

import com.example.yin.dao.CollectMapper;
import com.example.yin.dao.ConsumerSongListMapper;
import com.example.yin.dao.SongMapper;
import com.example.yin.domain.Collect;
import com.example.yin.domain.ConsumerSongList;
import com.example.yin.domain.Song;
import com.example.yin.service.CollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class CollectServiceImpl implements CollectService {
    @Autowired
    private CollectMapper collectMapper;
    @Autowired
    private ConsumerSongListMapper consumerSongListMapper;
    @Autowired
    private SongMapper songMapper;

    @Override
    public boolean addSongList(ConsumerSongList consumerSongList) {
        return consumerSongListMapper.addSongList(consumerSongList)>0;
    }//添加歌单

    @Override
    public int addSongToList(Collect collect) {
        Collect collect1 = collectMapper.selectByUSLId(collect.getUserId(), collect.getSongId(), collect.getSongListId());
        if(collect1 == null)
            return collectMapper.addCollect(collect);
        else return -1;
    }//收藏歌曲

    @Override
    public boolean deleteSongToList(Integer id) {
        return collectMapper.deleteById(id)>0;
    }//删除收藏歌曲

    @Override
    public List<Song> allSongs(Integer id) {
        List<Collect> collects = collectMapper.allSongs(id);
        List<Song> songs = new ArrayList<>();
        for(int i =0;i<collects.size();i++){
            songs.add(songMapper.selectById(collects.get(i).getSongId()));
        }
        return songs;
    }//返回自定义歌单中所有歌曲

    @Override
    public List<ConsumerSongList> allLists(Integer userId) {
        List<ConsumerSongList> consumerSongLists = consumerSongListMapper.selectByUserId(userId);
        return consumerSongLists;
    }//根据用户id返回用户所有歌单

    @Override
    public boolean deleteSongList(Integer id) {
        if(consumerSongListMapper.deleteById(id)>0){
            return collectMapper.deleteBySongListId(id)>=0;
        }else {
            return false;
        }
    }//用户删除歌单及歌单下的所有歌曲

    @Override
    public boolean updateSongList(ConsumerSongList consumerSongList) {
        ConsumerSongList consumerSongList1 = consumerSongListMapper.selectById(consumerSongList.getId());
        consumerSongList1.setStyle(consumerSongList.getStyle());
        consumerSongList1.setIntroduction(consumerSongList.getIntroduction());
        consumerSongList1.setTitle(consumerSongList.getTitle());
        return consumerSongListMapper.updateSongList(consumerSongList1)>0;
    }//更新歌单信息

    @Override
    public ConsumerSongList selectById(Integer id) {
        return consumerSongListMapper.selectById(id);
    }//根据id获取歌单信息

    @Override
    public int deleteBySongId(Integer userId, Integer songId, Integer songListId) {
        return collectMapper.deleteBySongId(userId, songId, songListId);
    }

    @Override
    public boolean updateSongListAvator(ConsumerSongList record) {
        return consumerSongListMapper.updateSongListAvator(record) >0 ?true:false;
    }

    @Override
    public List<ConsumerSongList> queryConsumerSongList(String str,Integer userId) {
        return consumerSongListMapper.queryConsumerSongList(str,userId);
    }

    @Override
    public List<Song> querySongInList(String str, Integer userId) {
        List<Collect> collects = collectMapper.allLists(userId);
        List<Song> songs = new ArrayList<>();
        for(int i =0;i<collects.size();i++){
            songs.add(songMapper.selectById(collects.get(i).getSongId()));
        }

        Pattern p = Pattern.compile(str);
        List<Song> querySongs = new ArrayList<>();
        for(int i = 0;i<songs.size();i++){
            Matcher m = p.matcher(songs.get(i).getName());
            boolean result = m.find();
            if(result==true)querySongs.add(songs.get(i));
        }
        return querySongs;
    }
}
